﻿(function (a) { a.fn.vAlign = function () { return this.each(function () { var b = a(this).height(), c = a(this).outerHeight(), b = (b + (c - b)) / 2; a(this).css("margin-top", "-" + b + "px"); a(this).css("top", "50%"); a(this).css("position", "absolute") }) } })(jQuery); (function (a) { a.fn.hAlign = function () { return this.each(function () { var b = a(this).width(), c = a(this).outerWidth(), b = (b + (c - b)) / 2; a(this).css("margin-left", "-" + b + "px"); a(this).css("left", "50%"); a(this).css("position", "absolute") }) } })(jQuery);

$(document).ready(function () {
	if ($('#login-wrapper').length) {
		$("#login-wrapper").vAlign().hAlign();
	};

	if ($('#login-validate').length) {
		$('#login-validate').validate({
			onkeyup: false,
			errorClass: 'error',
			rules: {
				login_name: {
					required: true,
					email: true
				},
				login_password: {
					required: true
				}
			},
			messages: {
				login_name: {
					required: 'Informe o email.',
					email: 'O email informado é inválido.'
				},
				login_password: {
					required: 'Informe a senha.',
				}
			},
			submitHandler: function (form) {
				$.ajax({
					url: 'Login/Logon',
					type: 'post',
					dataType: 'json',
					data: {
						Email: $('#login_name').val(),
						Senha: $('#login_password').val(),
						LembrarMe: $('#login_remember').is(':checked')
					},
					success: function (data) {
						if (data.messages.length) {
							showSummaryMessages(data.messages);
							return;
						}

						window.location = data.url;
					},
					error: function (data) {
						showSummaryMessages(data.statusCode + ' - ' + data.statusText);
					}
				});
			}
		});
	}

	if ($('#forgot-validate').length) {
		$('#forgot-validate').validate({
			onkeyup: false,
			errorClass: 'error',
			rules: {
				forgot_email: {
					required: true,
					email: true
				}
			},
			messages: {
				forgot_email: {
					required: 'Informe o email.',
					email: 'O email informado é inválido.'
				}
			}
		});
	}

	$('#pass_login').click(function () {
		$('#errorHandler').empty();
		$('.panel:visible').slideUp('200', function () {
			$('.panel').not($(this)).slideDown('200');
		});

		$(this).children('span').toggle();
	});

	$('#login_name').focus();
});

function showSummaryMessages(message) {
	$('#errorHandler').empty();
	var summary = $('<ul/>').append(message).html();
	$('#errorHandler').append('<div class="alert alert-error fade in"><a data-dismiss="alert" class="close">×</a>' + summary + '</div>');
}
